#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import os
import importlib
import inspect


def run(instance):
    for attr in dir(instance):  # 遍历father实例的所有属性和方法
        if attr.startswith('test_'):  # 判断是否是以test_前缀
            attrib = getattr(instance, attr)
            if callable(attrib):  # 判断是否是函数
                attrib()


if __name__ == '__main__':
    py_list = []
    for root, directory, files in os.walk('.'):
        for f in files:
            if f.endswith('.py') and f.startswith(
                    'test_'):  # 获取当前目录下所有test前缀和py文件
                # 将路径转换成导入的格式：Day100.test_demo
                ext_pth = os.path.join(root, f)[2:-3].replace('\\', '.')
                py_list.append(importlib.import_module(ext_pth))  # 导入并存入列表

    for m in py_list:
        for name, class_ in inspect.getmembers(m, inspect.isclass):
            print(name)
            sub = class_()  # 如果是类则实例化
            run(sub)  # 执行其中的test前缀的方法
        for name, func_ in inspect.getmembers(m, inspect.isfunction):
            print(name)
            # print(inspect.getfullargspec(func_))
            func_()  # 如果是函数则直接运行
